  
      use RFS_master_data, clear
	 
  	 gen signed_q 		= bs_clients*quantity
	 
	 
	 * generate average trade size at day-client level
	 bys call st0_date instrumentid: egen asize = mean(quantity)
	 bys call st0_date instrumentid: egen aOF   = total(signed_q)
	
	
	 duplicates drop call st0_date instrumentid, force
	   
	 gen sophs = 0
	 replace sophs = 1 if activeZ  == 1
	  
	 
	 bys call  instrumentid : egen sizeMED = median(asize) 
	 
	 gen size2 = 1
	 replace size2 = 2 if asize > sizeMED
	  

	 ** compute aggregate orderflow by order-type (small trades vs large trades)
	 forvalues i = 0/1 {
	 cap drop aux*
	 	 bys st0_date instrumentid : egen aux_1 = total(aOF)   if size2 == 1 & sophs == `i'
	 	 bys st0_date instrumentid : egen aux_2 = total(aOF)   if size2 == 2 & sophs == `i'
	 	 bys st0_date instrumentid : egen aux_T = total(aOF)   if 			   sophs == `i'
		 
	 	 bys st0_date instrumentid : egen aOF`i'_1  = min(aux_1)   
	 	 bys st0_date instrumentid : egen aOF`i'_2  = min(aux_2)   
	 	 bys st0_date instrumentid : egen aOF`i'_T  = min(aux_T)
	
		 replace aOF`i'_1 = 0 if aOF`i'_1 == .
		 replace aOF`i'_2 = 0 if aOF`i'_2 == .
		 replace aOF`i'_T = 0 if aOF`i'_T == .
		 
	 }
	
	
	 duplicates drop st0_date instrumentid, force
	 
	 	 forvalues i = 0/1 {
			bys st0_date: egen sort`i'_T = xtile(aOF`i'_T), nq(3)
			bys st0_date: egen sort`i'_1 = xtile(aOF`i'_1), nq(3)
			bys st0_date: egen sort`i'_2 = xtile(aOF`i'_2), nq(3)
		 }
	  
	  
	
	   	  * forvalues i =  1/ 30 { IN THE PAPER, WE USE 30-DAY HORIZON !

	 forvalues i = 1/20 {
 	      qui gen ret_`i'd    = 10000 *(log(price_d`i')    - log(price))   
	     
	 }
	  
  	 	 global vars4 "1 2 T"
		 
		 	 	 global vars45 "ret    "
	
	foreach jj of global vars45 {
		foreach j of global vars4 {
	    forvalues i =  1/ 20 {
  	  * forvalues i =  1/ 30 { IN THE PAPER, WE USE 30-DAY HORIZON !

	di "`jj'_`j'_`i'"
  qui {
 cap drop aux*
 
	 * UNWEIGHTED:
	 
		     bys st0_date: egen aux_lowU = mean(`jj'_`i'd) if sort0_`j' == 1
		 	 bys st0_date: egen aux_higU = mean(`jj'_`i'd) if sort0_`j' == 3
  
		     bys st0_date: egen aux_lowS = mean(`jj'_`i'd) if sort1_`j' == 1
		 	 bys st0_date: egen aux_higS = mean(`jj'_`i'd) if sort1_`j' == 3 
			 
				 			 * aggregate portfolio return
		 	 bys st0_date: egen aux_LU = min(aux_lowU)
		 	 bys st0_date: egen aux_HU = min(aux_higU)		 
		 	 bys st0_date: egen aux_LS = min(aux_lowS)
		 	 bys st0_date: egen aux_HS = min(aux_higS)				 
			 
		    gen `jj'U_`j'_`i'd = aux_HU - aux_LU
		    gen `jj'S_`j'_`i'd = aux_HS - aux_LS
			
 *  WEIGHTED:
	  cap drop aux*
	  gen auxw0 = abs(aOF0_`j')
	  gen auxw1 = abs(aOF1_`j')	  
		     bys st0_date: egen aux_lowU = wtmean(`jj'_`i'd), weight(auxw0) , if sort0_`j' == 1
		 	 bys st0_date: egen aux_higU = wtmean(`jj'_`i'd), weight(auxw0) , if sort0_`j' == 3
  
		     bys st0_date: egen aux_lowS = wtmean(`jj'_`i'd), weight(auxw1) , if sort1_`j' == 1
		 	 bys st0_date: egen aux_higS = wtmean(`jj'_`i'd), weight(auxw1) , if sort1_`j' == 3 
			 
				 			 * aggregate portfolio return
		 	 bys st0_date: egen aux_LU = min(aux_lowU)
		 	 bys st0_date: egen aux_HU = min(aux_higU)		 
		 	 bys st0_date: egen aux_LS = min(aux_lowS)
		 	 bys st0_date: egen aux_HS = min(aux_higS)				 
			 
		    gen `jj'Uw_`j'_`i'd = aux_HU - aux_LU
		    gen `jj'Sw_`j'_`i'd = aux_HS - aux_LS			

			
		} 
	   }
	 }
	}
	
	
	  duplicates drop st0_date, force
	 
	 compress
	   	  
 * Preset variables to missing for later replace for IRFS;
 cap drop h bC*  bT* up90b* lo90b*
  gen h = _n    /* h is the horizon for the IRFs */
  quietly gen bTRAN = . 
  quietly gen bCENT = . 
  quietly gen up90bCENT = . 
  quietly gen lo90bCENT = . 
  quietly gen up90bTRAN = . 
  quietly gen lo90bTRAN = . 
   
  
  	  * forvalues i =  1/ 30 { IN THE PAPER, WE USE 30-DAY HORIZON !

  	   forvalues i =  1/ 20 {
	di `i'
 
 qui {
 cap drop aux*
  
		 local j =  "T"   
		  
		 
	  	     gen auxU = retUw_2_`i'd - retUw_1_`i'd
	  	     gen auxS = retSw_2_`i'd - retSw_1_`i'd
	  
			 reg auxU , vce(robust)
			 	 gen aux1 = _b[_cons] 
				 gen aux2 = _se[_cons] 
	
			 reg auxS , vce(robust)
			 	 gen aux3 = _b[_cons] 
				 gen aux4 = _se[_cons] 
				 

***** create confidence bands  ****
 	scalar sig1 = 0.10	 // specify significance level
     
    quietly replace bCENT 		= aux1 if h ==`i' 
    quietly replace up90bCENT   = aux1 + invnormal(1-sig1/2)*aux2 if h==`i' 
	quietly replace lo90bCENT   = aux1 - invnormal(1-sig1/2)*aux2 if h==`i' 
	
    quietly replace bTRAN		= aux3 if h ==`i' 
    quietly replace up90bTRAN   = aux3 + invnormal(1-sig1/2)*aux4 if h==`i' 
	quietly replace lo90bTRAN   = aux3 - invnormal(1-sig1/2)*aux4 if h==`i' 	
	
	 
		} 
	   }
	  
 cap drop bzero
 gen bzero = 0
  
 **BASELINE:
  tw (scatter up90bCENT  h, c(l ) clp(dash )   ms(i )   clc(black) mc(black) clw(medthin))  (scatter lo90bCENT  h, c(l ) clp(dash ) ms(i ) clc(black) mc(black) clw(medthin))     ///
(scatter bCENT  h, c(l ) clp(l ) msize(1.3) clc(black) mc(black) clw(medthick))   (lfit bzero h, c(l ) clp(l )  clc(red) )   if h<=20 , ///
  graphregion(color(white)) legend(off) title("Less Sophisticated") xtitle("TRADING DAYS") name("BASELINE_DASH0", replace)
   
   
     tw (scatter up90bTRAN   h, c(l ) clp(dash )   ms(i )   clc(black) mc(black) clw(medthin))  (scatter lo90bTRAN   h, c(l ) clp(dash ) ms(i ) clc(black) mc(black) clw(medthin))     ///
(scatter bTRAN   h, c(l ) clp(l ) msize(1.3) clc(black) mc(black) clw(medthick))   (lfit bzero h, c(l ) clp(l )  clc(red) )   if h<=20 , ///
  graphregion(color(white)) legend(off)  title("More Sophisticated") xtitle("TRADING DAYS") name("BASELINE_DASH1", replace)
   
   
     graph combine BASELINE_DASH0 BASELINE_DASH1, ycommon  graphregion(color(white))  name("BalaTT0w22", replace) 
     graph export "RFS_IRF_information.pdf", replace

	 graph drop BASELINE_DASH0 BASELINE_DASH1
	 

	 
	   
 cap drop h bC*  bT* up90b* lo90b*
  gen h = _n    /* h is the horizon for the IRFs */
  quietly gen bTRAN = . 
  quietly gen bCENT = . 
  quietly gen up90bCENT = . 
  quietly gen lo90bCENT = . 
  quietly gen up90bTRAN = . 
  quietly gen lo90bTRAN = . 
   
   
	     
  	   forvalues i =  1/ 20 {
	di `i'
 
 qui {
 cap drop aux*
	 
		 
		 local j =  "T"   
		  
		 
	  	     gen auxU = retUw_2_`i'd - retUw_1_`i'd  
	  	     gen auxS = retSw_2_`i'd - retSw_1_`i'd  
	 
	 
	 
	 cap drop xx
	 gen xx = 2
	 replace xx = 1 if D4_NEWS == 1 | D4_NEWS == 3
	   
			   reg  auxS if xx == 1 , vce(robust)   
			 	 gen aux1 = _b[_cons] 
				 gen aux2 = _se[ _cons] 	
			
			 reg  auxS if xx == 2 , vce(robust)   				 
				 
	 			 gen aux3 = _b[_cons] 
				 gen aux4 = _se[_cons] 
	 
			  	 

***** create confidence bands (in this case 90 and 95%) ****
 	scalar sig1 = 0.10	 // specify significance level
    * di invnormal(1-sig1/2) 
    quietly replace bCENT 		= aux1 if h ==`i' 
    quietly replace up90bCENT   = aux1 + invnormal(1-sig1/2)*aux2 if h==`i' 
	quietly replace lo90bCENT   = aux1 - invnormal(1-sig1/2)*aux2 if h==`i' 
	
    quietly replace bTRAN		= aux3 if h ==`i' 
    quietly replace up90bTRAN   = aux3 + invnormal(1-sig1/2)*aux4 if h==`i' 
	quietly replace lo90bTRAN   = aux3 - invnormal(1-sig1/2)*aux4 if h==`i' 	
	 
		} 
	   }
	  
 cap drop bzero
 gen bzero = 0
  
 **BASELINE:
  tw (scatter up90bCENT  h, c(l ) clp(dash )   ms(i )   clc(black) mc(black) clw(medthin))  (scatter lo90bCENT  h, c(l ) clp(dash ) ms(i ) clc(black) mc(black) clw(medthin))     ///
(scatter bCENT  h, c(l ) clp(l ) msize(1.3) clc(black) mc(black) clw(medthick))   (lfit bzero h, c(l ) clp(l )  clc(red) )   if h<=20 , ///
  graphregion(color(white)) legend(off) title("Small News") xtitle("TRADING DAYS") name("BASELINE_DASH0", replace)
    
   
     tw (scatter up90bTRAN   h, c(l ) clp(dash )   ms(i )   clc(black) mc(black) clw(medthin))  (scatter lo90bTRAN   h, c(l ) clp(dash ) ms(i ) clc(black) mc(black) clw(medthin))     ///
(scatter bTRAN   h, c(l ) clp(l ) msize(1.3) clc(black) mc(black) clw(medthick))   (lfit bzero h, c(l ) clp(l )  clc(red) )   if h<=20 , ///
  graphregion(color(white)) legend(off)  title("Big News") xtitle("TRADING DAYS") name("BASELINE_DASH1", replace)
    
   
     graph combine BASELINE_DASH0 BASELINE_DASH1, ycommon  graphregion(color(white))  name("Bad", replace) 
            graph export "RFS_IRF_MACRO.pdf", replace

	 graph drop BASELINE_DASH0 BASELINE_DASH1
	 
	 